#include"head.h"
#define MAX_BUF_LEN 100
#define MAX_ARG_LEN 60

int read_stdin(order_t* order_s){
    //read(STDIN_FILENO, buf, sizeof(buf)-1);
    char* token;
    char order[MAX_ARG_LEN] ={0}; // 用于存储命令的数组
    char buf[MAX_BUF_LEN] = {0};

    // 提示用户输入
    printf("Enter command: \n");
    if (fgets(buf, sizeof(buf), stdin) == NULL) {
        fprintf(stderr, "Error reading input\n");
        return 1;
    }

    buf[strlen(buf)-1]=0;
    token = strtok(buf, " \t"); // 第一个token是命令
    if (token == NULL) {
        fprintf(stderr, "No command entered\n");
        return 1;
    }else{
        strcpy(order, token);
    }

    token = strtok(NULL, " \t");
    if(token == NULL){
        memset(order_s->buf, 0, sizeof(order_s->buf));
    }else strcpy(order_s->buf, token);

    printf("order:%s\n", order);
    printf("arg:%s\n", order_s->buf);
    order_s->kv_num = 1;
    /* printf("para: %s  %ld \n", para, strlen(para) ); */

    if(memcmp(order, "cd", 2) == 0){
        order_s->order = CD;
    }else if(memcmp(order, "ls", 2) == 0){
        order_s->order = LS;
    }else if(memcmp(order, "puts", 4) == 0){
        order_s->order = PUTS;
    }else if(memcmp(order, "gets", 4) == 0){
        order_s->order = GETS;
    }else if(memcmp(order, "rm", 2) == 0){
        order_s->order = RM;
    }else if(memcmp(order, "pwd", 3) == 0){
        order_s->order = PWD;
    }else if(memcmp(order, "mkdir", 5) == 0){
        order_s->order = MKDIR;
    }else if(memcmp(order, "rgst", 4) == 0){
        order_s->order = RGST;
    }else if(memcmp(order, "login", 5) == 0){
        order_s->order = LOGIN;
    }else order_s->order = -1;

    return 0;
}

/* read(STDIN_FILENO, buf, sizeof(buf)); */
/* scanf("%s ", buf); */

/* int index=0; */
/* for(int i=0; i<100; i++){ */
/*     if(buf[i] != ' '){ */
/*         order[i]=buf[i]; */
/*     }else{ */
/* index=i; */
/* break; */
/* } */
/* } */

/* char para[100]={0}; */
/* for(int i=index+1; i<100; i++){ */
/*     if(buf[i]!=' '){ */
/*         order_s->buf[i-index-1]=buf[i]; */
/*     }else{ */
/*         break; */
/* } */
/* } */
